<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class Staff extends Model{
    protected  $table = 'staff';
    /**
     * @param $page             页码
     * @param $rows             一页的列数
     * @param $keyword          关键字
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function onePage($page, $rows, $keyword) {
        $where = array(
            'name' => array('like', '%'.$keyword.'%'),
            'job_number' => array('like', '%'.$keyword.'%')
        );
        $data=Db::name($this->table)
            ->whereOr($where)
            ->page($page, get_list_rows())
            ->field('staff_id,name,job_number,sex,tel,department_id,department_name')
            ->order('create_time desc')
            ->select();
        return $data;
    }

    /**
     * @param $keyword          关键字
     * @return int|string       总数
     * @throws \think\Exception
     */
    public function total($keyword) {
        $where = array(
            'name' => array('like', '%'.$keyword.'%'),
            'job_number' => array('like', '%'.$keyword.'%')
        );
        $data=Db::name($this->table)
            ->whereOr($where)
            ->count();
        if (gettype($data) !== 'integer') die;
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($staff_id, $data) {
        $data = Db::name($this->table)
            ->where(['staff_id'=>$staff_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($staff_id) {
        $data = Db::name($this->table)
            ->where(['staff_id'=>$staff_id])
            ->delete();
        return $data;
    }

    /**
     * @param $staff_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($staff_id) {
        $data = Db::name($this->table)
            ->where(['staff_id'=>$staff_id])
            ->field('staff_id, name, job_number, sex, tel, 
            department_id, $department_name, $role_id, $role_name, 
            $create_time, update_time')
            ->find();
        return $data;
    }

    /**
     * @param $staff_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getKey($staff_id) {
        $data = Db::name($this->table)
            ->where(['staff_id'=>$staff_id])
            ->field('staff_id, key')
            ->find();
        return $data;
    }
}